/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <a class="back" href="javascript:history.back(-1)" title="返回列表">
        <i class="fa fa-arrow-circle-o-left"></i>
      </a>
      <div class="subject">
        <h3>微店管理</h3>
        <h5>商城分销商品管理</h5>
      </div>
    </div>
    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>参与分销的商品在此列表显示</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>
    <div class="mDiv">
      <div class="ftitle">
        <h3>分销商品列表</h3>
        <h5>(共 {{totalCount}} 条记录)</h5>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
      <div class="searchbox">
        <el-cascader
          v-model="categoryId"
          :options="goodsCategoryTree"
          :props="{label: 'name', value: 'id',expandTrigger: 'hover',checkStrictly: true}"
          clearable
          filterable
          size="small"
          placeholder="所有分类"
        />
        <el-select v-model="brandId" size="small" clearable filterable placeholder="所有品牌">
          <el-option v-for="item in brand" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-input
          class="searchinput"
          v-model="goodsNameOrSn"
          width="10"
          size="small"
          placeholder="商品名称或者货号"
          clearable
        />
        <el-button
          class="searchbtn"
          size="small"
          icon="el-icon-search"
          @click="currentPage = 1;getPage()"
        >搜索</el-button>
      </div>
    </div>
    <div class="tablebox" v-if="tableData">
      <el-table :data="tableData" ref="multipleTable" style="width: 100%;">
        <el-table-column label="分销ID" prop="user_distribution.id" width="80"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="300" show-overflow-tooltip></el-table-column>
        <el-table-column label="分类" prop="goods_category.name" width="120"></el-table-column>
        <el-table-column label="价格" prop="shop_price" width="100" :formatter="toFixedShopPrice"></el-table-column>
        <el-table-column label="库存" prop="store_count" width="100"></el-table-column>
        <el-table-column label="总销量" prop="sales_sum" width="100"></el-table-column>
        <el-table-column label="分销销量" prop="user_distribution.sales_num" width="100"></el-table-column>
        <el-table-column label="分享数" prop="user_distribution.share_num" width="100"></el-table-column>
        <el-table-column label="分成金额" prop="distribut" width="100" :formatter="toFixedDistribut"></el-table-column>
        <el-table-column label="占商品价格比" prop="distribut_proportion" width="130"></el-table-column>
        <el-table-column label="商品加入时间" prop="user_distribution.add_time_desc" width="150"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button size="small" @click="detail(scope.row.goods_id)" icon="el-icon-zoom-in">查看</el-button>
              <el-button size="small" @click="remove(scope.row.goods_id)" icon="el-icon-delete">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        class="paginationbox"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getMyStoreManage,
  getGoodsCategoryTree,
  getBrandList,
  deleteDistributionGoods
} from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      currentPage: this.$route.params.currentPage || 1,
      totalCount: 0,
      pageSize: 10,
      goodsCategoryTree: [],
      categoryId: [],
      brand: [],
      brandId: null,
      goodsNameOrSn: null
    };
  },
  created() {
    this.getPage();
    this.getGoodsCategoryTree();
    this.getBrand();
  },
  methods: {
    handleSizeChange(e) {
      this.pageSize = e;
      this.getPage();
    },
    handleCurrentChange(e) {
      this.currentPage = e;
      this.getPage();
    },
    getPage() {
      var that = this;
      getMyStoreManage({
        user_id: this.$route.params.user_id,
        cat_id: this.categoryId[this.categoryId.length - 1],
        brand_id: this.brandId,
        goods_name_or_sn: this.goodsNameOrSn,
        p: this.currentPage,
        size: this.pageSize
      }).then(function(res) {
        res.records.forEach(e => {
          e.distribut_proportion =
            Math.round((e.distribut / e.shop_price) * 10000) / 100 + "%";
        });
        that.tableData = res.records;
        that.totalCount = res.total;
      });
    },
    refresh() {
      this.getPage();
      this.$message({
        type: "success",
        message: "刷新成功！"
      });
    },
    getGoodsCategoryTree() {
      var that = this;
      getGoodsCategoryTree({
        levels: "1,2,3"
      }).then(function(res) {
        that.goodsCategoryTree = res;
      });
    },
    getBrand() {
      var that = this;
      getBrandList().then(function(res) {
        that.brand = res;
      });
    },
    detail(goodsId) {
      this.$message.info("施工中...");
    },
    remove(goodsId) {
      var that = this;
      this.$confirm("确定删除该分销商品？ 将恢复成普通商品", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteDistributionGoods(goodsId);
          that.request
            .delete("/mall/distribution?goods_id=" + goodsId)
            .then(function(res) {
              if (res.status == 1) {
                that.getPage();
                that.$message({
                  type: "success",
                  message: "删除成功!"
                });
              } else {
                that.$message.error({
                  message: res.msg
                });
              }
            });
        })
        .catch(() => {
          that.$message.info({
            message: "已取消删除"
          });
        });
    },
    toFixedShopPrice(row) {
      return row.shop_price.toFixed(2);
    },
    toFixedDistribut(row) {
      return row.distribut.toFixed(2);
    }
  }
};
</script>

<style scoped>
.searchinput {
  width: auto;
}
</style>
